using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataService.Helper;
using DataService.Helper.Kafka;
using OpcuaClient.Model;

namespace OpcuaClient.Service
{
    public class MessageService
    {
        LogHelper LogHelper = new LogHelper("OpcuaClient");
        ConstructService ConstructService = new ConstructService();
        public MessageService()
        {
            ConstructService.AfterSendMessage += new EventHandler(SendMessage);
        }
        object obj = new object();
        private void SendMessage(object obj, EventArgs e)
        {
            if (obj == null) return;
            lock (obj)
            {
                if (obj == null) return;
                Message message;
                try
                {
                    message = (Message)obj;
                    KafkaProducer kafkaProducer = new KafkaProducer();
                    kafkaProducer.Produce(message.Server, message.Topic, message.Value.ToString());
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex.Message);
                }
            }
        }
    }
}
